﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace GabageFactory.Models
{
    public class PiezometerRecordReposity : Reposity
    {
        public List<Piezometer_well_monthly_info> GetMonthlyPiezometerWellInfo(int stackId, DateTime date)
        {
            var infos = from s in _dataContext.Piezometer_well_monthly_infos
                                          where stackId == s.Stack_id && s.date == date.Date
                                          orderby s.piezometer_well_id ascending
                                          select s;

            if (infos != null && infos.Count() > 0)
            {
                return infos.ToList();
            }
            else
            {
                return null;
            }
        }

        public IDictionary<DateTime, Piezometer_well_monthly_info> QueryWellInfo(int stackId, int wellId, DateTime dateFrom, DateTime dateTo)
        {
            var wells = from s in _dataContext.Piezometer_well_monthly_infos
                        where stackId == s.Stack_id && s.date >= dateFrom && s.date < dateTo && s.piezometer_well_id == wellId
                        select s;

            IDictionary<DateTime, Piezometer_well_monthly_info> infos = new Dictionary<DateTime, Piezometer_well_monthly_info>();
            foreach (Piezometer_well_monthly_info info in wells)
            {
                infos.Add(info.date, info);
            }

            return infos;
        }


        public IDictionary<DateTime, Piezometer_monthly_info> QueryEquapInfo(int stackId, int holeId, DateTime dateFrom, DateTime dateTo)
        {
            var equaps = (from s in _dataContext.Piezometer_monthly_infos
                         where stackId == s.Stack_id && s.date >= dateFrom && s.date < dateTo && s.piezometer_hole_id == holeId
                                              select new { s.date, s.Stack_id, s.piezometer_hole_id, s.Hydraulic_gradient }).Distinct();

            IDictionary<DateTime, Piezometer_monthly_info> infos = new Dictionary<DateTime, Piezometer_monthly_info>();
            foreach (var info in equaps)
            {
                Piezometer_monthly_info record = new Piezometer_monthly_info();
                record.date = info.date;
                record.Stack_id = info.Stack_id;
                record.piezometer_hole_id = info.piezometer_hole_id;
                record.Hydraulic_gradient = info.Hydraulic_gradient;
                infos.Add(info.date, record);
            }

            return infos;
        }

        public IDictionary<DateTime, Piezometer_monthly_info> QueryEquapPressureInfo(int stackId, int holeId, int piezometerId, DateTime dateFrom, DateTime dateTo)
        {
            var equaps = from s in _dataContext.Piezometer_monthly_infos
                          where stackId == s.Stack_id && s.date >= dateFrom && s.date < dateTo && s.piezometer_hole_id == holeId && s.piezometer_id == piezometerId
                          select s;

            IDictionary<DateTime, Piezometer_monthly_info> infos = new Dictionary<DateTime, Piezometer_monthly_info>();
            foreach (var info in equaps)
            {
                infos.Add(info.date, info);
            }

            return infos;
        }


        public Piezometer_well_monthly_info CreatePiezometerWellRecord(Piezometer_well_monthly_info info)
        {
            _dataContext.Piezometer_well_monthly_infos.InsertOnSubmit(info);
            SubmitChanges();
            return info;
        }

        public bool DeletePiezometerWellInfos(int stackId, DateTime date)
        {
            var infos = from s in _dataContext.Piezometer_well_monthly_infos
                        where stackId == s.Stack_id && s.date == date.Date
                        select s;
            foreach (Piezometer_well_monthly_info info in infos)
            {
                _dataContext.Piezometer_well_monthly_infos.DeleteOnSubmit(info);
            }
            return true;
        }

        public List<Piezometer_monthly_info> GetMonthlyPiezometerBaseInfo(int stackId, DateTime date)
        {
            var infos = from s in _dataContext.Piezometer_monthly_infos
                        where stackId == s.Stack_id && s.date == date.Date
                        orderby s.piezometer_hole_id ascending
                        select s;

            if (infos != null && infos.Count() > 0)
            {
                return infos.ToList();
            }
            else
            {
                return null;
            }
        }

        public Piezometer_monthly_info CreatePiezometerBaseRecord(Piezometer_monthly_info info)
        {
            _dataContext.Piezometer_monthly_infos.InsertOnSubmit(info);
            SubmitChanges();
            return info;
        }

        public bool DeletePiezometerBaseInfos(int stackId, DateTime date)
        {
            var infos = from s in _dataContext.Piezometer_monthly_infos
                        where stackId == s.Stack_id && s.date == date.Date
                        select s;
            foreach (Piezometer_monthly_info info in infos)
            {
                _dataContext.Piezometer_monthly_infos.DeleteOnSubmit(info);
            }
            return true;
        }
    }
}